<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-07-31
 * Time: 10:24
 */

namespace App\Repositories;

use Illuminate\Support\Facades\Auth;
use Spatie\Permission\Models\Permission as PermissionSpate;
use App\Models\Permissions;
use Illuminate\Support\Arr;

/**
 * 遵循SOLID原则（分割、单元测试、mock测试）
 * 基于Permissions和Spatie\Permission\Models\Permission model的数据库逻辑
 * Class PermissionRepository
 * @package app\Repositories\Admin
 */
class PermissionRepository
{
    /**
     * @var Permissions 注入的Permissions model
     */
    protected $permission;

    /**
     * @var PermissionSpate 注入的PermissionSpate model
     */
    protected $permission_spate;

    /**
     * PermissionRepository constructor.
     * @param Permissions $permission
     * @param PermissionSpate $permission_spate
     */
    public function __construct(Permissions $permission, PermissionSpate $permission_spate){
        $this->permission = $permission;
        $this->permission_spate = $permission_spate;
    }

    /**
     * 获取权限路由列表
     * @param array $data
     * @param $limit
     * @return mixed
     */
    public function getPermissionRouterPage(array $data, $limit){
        return $this->permission
            ->when($data, function ($query) use ($data){
                $query->where($data);
            })
            ->orderByDesc('created_at', 'updated_at')
            ->paginate($limit);

    }

    /**
     * 保存权限路由
     * @param array $addData
     * @return bool
     */
    public function addPermissionRouter(array $addData){
        $this->permission->name = data_get($addData, 'name');
        $this->permission->description = data_get($addData, 'description');
        $this->permission->type = data_get($addData, 'type');
        $this->permission->parent_id = data_get($addData, 'parent_id', 0);
        $this->permission->guard_name = data_get($addData, 'guard_name', 'api');

        return $this->permission->saveOrFail();
    }

    /**
     * 更新权限
     * @param array $updateData
     * @return bool
     */
    public function updatePermissionRouter(array $updateData){

        $permission = $this->permission->findOrFail($updateData['id']);

        Arr::has($updateData, 'type') && $permission->type = $updateData['type'];
        Arr::has($updateData, 'parent_id') && $permission->parent_id = $updateData['parent_id'];
        Arr::has($updateData, 'description') && $permission->description = $updateData['description'];
        data_get($updateData, 'name') && $permission->name = $updateData['name'];
        data_get($updateData, 'guard_name') && $this->permission->guard_name = $updateData['guard_name'];

        return $permission->saveOrFail();
    }

    /**
     * 删除路由权限
     * @param array $ids
     * @return int
     */
    public function deletePermissionRouter(array $ids){
        return $this->permission->destroy($ids);
    }

    /**
     * 根据IDS查看所有的权限
     * @param array $ids
     * @return mixed
     */
    public function getAllPermissionByIds(array $ids){
        return $this->permission_spate->whereIn('id', $ids)->get();
    }

    /**
     * 获取所有权限路由列表
     * @param array $data
     * @return mixed
     */
    public function choicePermission(array $data){
        return $this->permission
            ->when($data,function ($q)use($data){
                $q->where($data);
            })
            ->get();
    }

    /**
     * 获取单条权限路由数据
     * @param int $id
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|static|static[]
     */
    public function singlePermission(int $id){
        return $this->permission
                ->with(['parent'=>function($query){
                    $query->with(['parent'=>function($query){
                        $query->with(['parent'=>function($query){
                            $query->select(['id', 'name', 'parent_id', 'type', 'description']);
                        }])->select(['id', 'name', 'parent_id', 'type', 'description']);
                    }])->select(['id', 'name', 'parent_id', 'type', 'description']);
                }])
                ->select('id', 'name', 'parent_id', 'type', 'description')
                ->findOrFail($id);
    }

    /**
     * 结构性数据
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function structurePermission(){
        return $this->permission
            ->with(['children'=>function($query){
                $query->with(['children'=>function($query){
                    $query->with(['children'=>function($query){
                        $query->where('type',1);
                        }]);
                }]);
            }])
            ->where(['type'=>0, 'parent_id'=>0])
            ->get(['id', 'name', 'parent_id', 'description']);
    }

}